#include "VOutlineWidget.h"
#include "ui_VOutlineWidget.h"
#include <QDebug>

VOutlineWidget::VOutlineWidget(QWidget *parent) :
    QTreeWidget(parent),
    ui(new Ui::VOutlineWidget)
{
    ui->setupUi(this);
    this->setHeaderHidden(true);
    this->setObjectName("project_treewidget");
    connect(this, &QTreeWidget::doubleClicked, this, &VOutlineWidget::on_treeWidget_doubleClicked);
}

VOutlineWidget::~VOutlineWidget()
{
    delete ui;
}

void VOutlineWidget::slot_current_path_changed(QString path)
{
    qDebug() << "setpath" << path;
    _path = path;
    if(path.isEmpty() == false)
        emit signal_outline_requst(path, -1);
    else
        this->clear();
}

void VOutlineWidget::slot_update_outline(QList<QTreeWidgetItem *> list, int version)
{
    this->clear();
    this->addTopLevelItems(list);
    _version = version;
}


void VOutlineWidget::on_treeWidget_doubleClicked(const QModelIndex &index)
{
    auto item = this->currentItem();
    if(item == nullptr || item->parent() == nullptr) return;

    int line = item->data(0, Qt::UserRole).value<int>();
    emit signal_jump(_path, line);
}

void VOutlineWidget::focusInEvent(QFocusEvent *event)
{
    if(_path.isEmpty() == false)
        emit signal_outline_requst(_path, _version);
}

